﻿using System;

namespace Kurax.BoardGames.Server.Engine.Commands {
    public sealed class RequestSessionKey : CommandBase {
        public override byte[] Execute(Context context, byte[] clientKey) {
            var serverKey = new byte[16];
            var random = new Random();
            random.NextBytes(serverKey);

            var sessionKey = new byte[16];
            for(var i = 0; i < sessionKey.Length; i++)
                sessionKey[i] = Convert.ToByte(clientKey[i] ^ serverKey[i]);

            context.Credentials.SessionKey = sessionKey;

            var crypto = new Crypto();
            return crypto.Encrypt(serverKey, clientKey);
        }
    }
}
